package com.sx.pipeline.ui.advertise.goods

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.view.SpaceItemDecoration2
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.GoodsManageAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActAdvertisingGoodsBinding
import com.sx.pipeline.repos.http.res.GoodsInfoVo
import com.sx.pipeline.ui.advertise.AdvertisingVM


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 广告宣传 商品管理
 */
@Route(path = RoutePath.Main.ADVERTISING_GOODS)
class AdvertisingGoodsActivity :
    CYDataBindingBaseActivity<ActAdvertisingGoodsBinding, AdvertisingVM>(R.layout.act_advertising_goods), View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var unitId: Int? = 0
    private var key=""
    private lateinit var mAdapter: GoodsManageAdapter

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mBinding.titleBar.rightView.setOnClickListener {
            RouteManager.Mine.intentToGoodsAdd(1,0)
        }
        Logs.d("ofjsdoafj"+unitId)
        //请求商品列表
        unitId?.let { mViewModel.getAdvertisingDetail(it) }
    }

    private fun initRv() {
        if (!this::mAdapter.isInitialized) {
            mAdapter = GoodsManageAdapter()
            mBinding.recyclerView.layoutManager =
                GridLayoutManager(this, 2)
            mBinding.recyclerView.addItemDecoration(
                SpaceItemDecoration2(20)
            )
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var info = adapter.data[position] as GoodsInfoVo
            RouteManager.Mine.intentToGoodsDetail(info.id,1)
        }
        mBinding.recyclerView.adapter = mAdapter
    }


    override fun initViewObservable() {
        super.initViewObservable()

        mViewModel.deviceListEvent.observe(this) {
            initRv()
            Glide.with(this).load(it?.image).into(mBinding.ivBg)
            val taskList = it?.productList as List<GoodsInfoVo>
            if (mViewModel.page == 1) {
                if (taskList.isNotEmpty()) {
                    mAdapter?.data = taskList as MutableList<GoodsInfoVo>
                    mBinding.recyclerView.visibility = View.VISIBLE
                    mBinding.emptyView.visibility = View.GONE
                } else {
                    mBinding.recyclerView.visibility = View.GONE
                    mBinding.emptyView.visibility = View.VISIBLE
                }
                mBinding.refreshLayout.finishRefresh()
            } else {
                if (taskList.isNotEmpty()) {
                    mAdapter?.addData(taskList)
                    mBinding.refreshLayout.finishLoadMore()
                } else {
                    mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
                }
            }
        }

        observe(mViewModel.advertiseIntroEvent){
            RouteManager.Register.intentToAgreement(it!!)
        }

    }

    override fun initListener() {
        super.initListener()
        mBinding?.refreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
//                mViewModel.page += 1
//                unitId?.let { mViewModel.getAdvertisingDetail(it) }
                mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page = 1
                unitId?.let { mViewModel.getAdvertisingDetail(it) }
            }
        })

        //广告公司介绍
        mBinding.ivBg.setOnClickListener {
            unitId?.let { it1 -> mViewModel.getAdvertisingIntro(it1) }
        }


        mBinding.etKeyword.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(
                s: CharSequence,
                start: Int,
                before: Int,
                count: Int
            ) {

            }

            override fun beforeTextChanged(
                s: CharSequence,
                start: Int,
                count: Int,
                after: Int
            ) {

            }

            override fun afterTextChanged(s: Editable) {
                key=mBinding.etKeyword.text.toString()
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {

            //搜索
            R.id.tv_search->{
                mViewModel.page=1
                unitId?.let { mViewModel.getAdvertisingDetail(it) }
            }
        }
    }

}